/**
 * 
 */
package cream.alarm.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.util.Log;

/**
 * 日期工具类
 * 
 * @author Cream
 * @since 1.0 2012-01-24
 */
public abstract class DateUtil {

	public static final String DATE_FORMAT = "yyyy-MM-dd";
	private static final String DATE_FORMAT_WITH_WEEK = "yyyy-MM-dd E";

	private static final DateFormat df = new SimpleDateFormat(DATE_FORMAT);
	private static final DateFormat dfw = new SimpleDateFormat(
			DATE_FORMAT_WITH_WEEK);
	private static final String TAG = "DateUtil";

	/**
	 * 判断日期是否为工作日
	 * 
	 * @param date
	 * @return
	 */
	public static boolean isWeekday(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return isWeekday(cal);
	}

	/**
	 * 判断日期是否为工作日
	 * 
	 * @param cal
	 * @return
	 */
	public static boolean isWeekday(Calendar cal) {
		int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
		return dayOfWeek > 1 && dayOfWeek < 7;
	}

	/**
	 * 提取时间的日期部分
	 * 
	 * @param date
	 * @return
	 */
	public static Date getDate(Date date) {
		String dateStr = df.format(date);
		try {
			return df.parse(dateStr);
		} catch (ParseException e) {
			Log.e(TAG, "Unknow expect exception catched!");
			throw new RuntimeException(e);
		}
	}

	/**
	 * 提取时间的日期部分
	 * 
	 * @param cal
	 * @return
	 */
	public static Calendar getDate(Calendar cal) {
		Calendar newCal = Calendar.getInstance();
		newCal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
				cal.get(Calendar.DAY_OF_MONTH));
		return newCal;
	}

	/**
	 * 按 {@link this#DATE_FORMAT} 格式进行日期解析。
	 * 
	 * @param dateStr
	 * @return
	 * @throws ParseException
	 */
	public static Date parse(String dateStr) throws ParseException {
		return df.parse(dateStr);
	}

	/**
	 * 按 {@link this#DATE_FORMAT} 格式对日期进行格式化。
	 * 
	 * @param date
	 * @return
	 */
	public static String format(Date date) {
		return df.format(date);
	}

	/**
	 * 按 {@link this#DATE_FORMAT_WITH_WEEK} 格式对日期进行格式化。
	 * 
	 * @param date
	 * @return
	 */
	public static String formatWithWeek(Date date) {
		return dfw.format(date);
	}

	/**
	 * 获取日历类，仅包含日期信息
	 * 
	 * @param date
	 * @return
	 */
	public static Calendar getCalendar(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getDate(date));
		return cal;
	}
}
